package webdemo;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import demo.SinaLevel2Deamon;

public class SinaLevel2Demo3
{
	
	private NewLevel2HandlerConcat level2_handler = new NewLevel2HandlerConcat();
	
	private SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
	public SinaLevel2Demo3(){
		File file = new File("./data");
		if(file.exists() == false){
			file.mkdir();
		}
		level2_handler.start("./data/"+df.format(new Date()));
	}
	public void stopAstock(String stockKey){
		SinaLevel2Deamon deamon = SinaLevel2Deamon.getInstance();
		deamon.stop(stockKey);
	}
	
	public void startDemo(int fetchType,String fetchMsg){
		List<String> list = new ArrayList<String>();
		String[] stockList = fetchMsg.split(",");
		for(String str:stockList){
			list.add(str);
		}
		SinaLevel2Deamon deamon = SinaLevel2Deamon.getInstance();
		deamon.start(list, level2_handler,fetchType);
	}

	public String getTodayData(String stockKey) {
		Map<String,Integer[]> map = this.level2_handler.getStockStaticInfo();
		StringBuilder builder = new StringBuilder();
		for(Entry<String,Integer[]> entryTemp : map.entrySet()){
			if(stockKey !=null){
			   if(false ==entryTemp.getKey().equals(stockKey)){
				  continue; 
			   }
			}
			builder.append(entryTemp.getKey()+",");
			for(Integer value : entryTemp.getValue()){
				builder.append(value+"|");
			}
		} 
		return builder.toString();
	}
	
	
}
